package org.kazuhiko.rpg.display;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import org.kazuhiko.rpg.core.Game;

public class LoginFrame extends JFrame
{
	private static final long serialVersionUID = -615610802527419966L;
	
	private JLabel lblUsername;
	private JLabel lblPassword;
	private JLabel lblWorlds;
	
	private JTextField txtUsername;
	
	private JPasswordField txtPassword;
	
	private JComboBox<String> cbWorlds;
	
	private JButton btnLogin;
	
	public LoginFrame()
	{		
		setTitle( GameScreen.GAME_NAME + ": Login" );
		setPreferredSize( new Dimension( 800, 600 ) );
		setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE ); //TODO: change back to HIDE_ON_CLOSE
		setResizable( false );
		setLayout( null );
		setLocationRelativeTo( null );
		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation( screenSize.width / 2 - 400, screenSize.height / 2 - 300 );
		
		init();
	}
	
	private void init()
	{
		lblUsername = new JLabel();
		lblUsername.setText( "Username: " );
		lblUsername.setBounds( 250, 180, 80, 20 );
		add( lblUsername );
		
		txtUsername = new JTextField();
		txtUsername.setBounds( 330, 180, 120, 20 );
		add( txtUsername );
		
		lblPassword = new JLabel();
		lblPassword.setText( "Password: " );
		lblPassword.setBounds( 250, 220, 80, 20 );
		add( lblPassword );
		
		txtPassword = new JPasswordField();
		txtPassword.setBounds( 330, 220, 120, 20 );
		txtPassword.addKeyListener( new KeyListener()
		{	
			@Override
			public void keyTyped( KeyEvent ke )
			{
			}
			
			@Override
			public void keyReleased( KeyEvent ke )
			{
				if( ke.getKeyCode() == KeyEvent.VK_ENTER )
				{
					tryLogin();
				}
			}
			
			@Override
			public void keyPressed( KeyEvent ke )
			{
			}
		});
		add( txtPassword );
		
		lblWorlds = new JLabel();
		lblWorlds.setText( "Select world: " );
		lblWorlds.setBounds( 250, 260, 80, 20 );
		add( lblWorlds );
		
		//TODO: get worlds from server
		cbWorlds = new JComboBox<String>();
		cbWorlds.addItem( "Default world" );
		cbWorlds.setBounds( 330, 260, 120, 20 );
		add( cbWorlds );
		
		btnLogin = new JButton();
		btnLogin.setText( "Login" );
		btnLogin.setBounds( 350, 300, 100, 20 );
		btnLogin.addActionListener( new ActionListener()
		{	
			@Override
			public void actionPerformed( ActionEvent ae )
			{
				tryLogin();
			}
		});
		add( btnLogin );
	}
	
	@SuppressWarnings("deprecation")
	private boolean checkLogin()
	{
		GameScreen.getClient().getSession().getActionSender().sendAuthenticationRequest( txtUsername.getText(), txtPassword.getText() );
		return true;
	}
	
	private void tryLogin()
	{
		if( checkLogin() )
		{
			Game.getGame().setUsername( txtUsername.getText() );
			
			setVisible( false );
			
			GameScreen.startUp();
		}
	}
	
	public void showFrame()
	{
		pack();
		setVisible( true );
	}
}